<?php
    include(dirname(__FILE__)."/../../../Dao/db.php");

    class KeywordTimerService
    {
        var $memberId;
        //メディアー種類
        var $listingType;
        //メディア名
        var $listingName;
        //キャンペーンID
        var $camp_Id;
        //キャンペーン名
        var $campaignName;
	//タイマーID
        var $timerId;
        //広告グループID
        var $adGr_Id;
        //広告グループ名
        var $adGroupName;
        //キーワードID
        var $key_Id;
        //キーワードテキスト
        var $keywordText;
        //タイマー実行フラグ　オン／オフ
        var $timerFlg;
        //タイマー種類：0[毎日]・1[毎週]・2[一回]
        var $timerType;
        //タイマー曜日
        var $timerWeek;
        //タイマー日付（一回の場合）
        var $timerDate;
        //タイマー時間
        var $timerTime;
	//検索件数
	var $count;
        //キーワード掲載状態
        var $keywordStatus;
        //入札額
        var $maxCpc;
        //登録内容
        var $task;

        function KeywordTimerService()
        {
//            session_start();
            $this->memberId = $_SESSION['memberId'];

            $this->listingType = $_GET['listingType'];
            $this->listingName = $this->getListingName();

            $this->camp_Id = $_GET['camp_Id'];
            $this->campaignName = $this->getCampaignName();

	    $this->timerId = $_GET['timerId'];

            $this->adGr_Id = $_GET['adGr_Id'];
            $this->adGroupName = $this->getAdGroupName();

            $this->key_Id = $_GET['key_Id'];
            $this->keywordText = $this->getKeywordText();

            $this->timerFlg = $_POST['timerFlg'];
            $this->timerType = $_POST['timerType'];

            //タイマー型は曜日以外場合、曜日のフィールドはNULLで設定する
            $this->timerWeek = "";
            if($this->timerType != 1)
            {
                $this->timerWeek = "";
            }
            else
            {
                if(isset($_POST['Sun']))
                {
                    $this->timerWeek .= "0";
                }
                if(isset($_POST['Mon']))
                {
                    $this->timerWeek .= "1";
                }
                if(isset($_POST['Tue']))
                {
                    $this->timerWeek .= "2";
                }
                if(isset($_POST['Wed']))
                {
                    $this->timerWeek .= "3";
                }
                if(isset($_POST['Thu']))
                {
                    $this->timerWeek .= "4";
                }
                if(isset($_POST['Fri']))
                {
                    $this->timerWeek .= "5";
                }
                if(isset($_POST['Sat']))
                {
                    $this->timerWeek .= "6";
                }
            }

            if(isset($_POST['timerDate']))
            {
                $this->timerDate = $_POST['timerDate'];
            }
            else
            {
                $this->timerDate = "";
            }
            if($this->timerType == 0)
            {
                $this->timerTime = $_POST['timerTime0'];
            }
            else if($this->timerType == 1)
            {
                $this->timerTime = $_POST['timerTime1'];
            }
            else if($this->timerType == 2)
            {
                $this->timerTime = $_POST['timerTime2'];
            }

            $this->keywordStatus = $_POST['status'];
            if(isset($_POST['maxCpc']) && $_POST['maxCpc'] !== "")
            {
                $this->maxCpc = $_POST['maxCpc'];
            }
            else
            {
                $this->maxCpc = "null";
            }

            $this->task = "";
            if(isset($_POST['change_status']))
            {
                $this->task .= "0";
            }
            if(isset($_POST['change_maxcpc']))
            {
                $this->task .= "1";
            }
        }

        //媒体種類により、媒体名を取得する
        function getListingName()
        {
            $query = "SELECT name FROM listingmst WHERE ";
            $query .= "id=$this->listingType";
            $result = mysql_query($query);
            $row = mysql_fetch_array($result);
            return $row['name'];
        }

        //キャンペーンIDにより、キャンペンーン名を取得する
        function getCampaignName()
        {
            $query = "SELECT name FROM campaign WHERE ";
            $query .= "camp_Id=$this->camp_Id";
            $result = mysql_query($query);
            $row = mysql_fetch_array($result);
            return $row['name'];
        }

        //広告グループIDにより、広告グループ名を取得する
        function getAdGroupName()
        {
            $query = "SELECT name FROM adgroup WHERE ";
            $query .= "adGr_Id=$this->adGr_Id";
            $result = mysql_query($query);
            $row = mysql_fetch_array($result);
            return $row['name'];
        }

        //キーワードIDにより、キーワードテキストを取得する
        function getKeywordText()
        {
            $query = "SELECT text FROM keyword WHERE ";
            $query .= "key_Id=$this->key_Id";
            $result = mysql_query($query);
            $row = mysql_fetch_array($result);
            return $row['text'];
        }

        //タイマーをDBに登録する
        function addTimer()
        {
	    $now = date("Y-m-d H:i:s");
            $query = "INSERT INTO TimerKeywordSchedule (";
            $query .= "memberId,key_Id,EnableFlg,";
            $query .= "TimerType,Week,Date,Time,AppearFlg,";
            $query .= "maxCpc,Task,";
	    $query .= "registDate,lastUpdate) ";
            $query .= "VALUES (";
            $query .= "$this->memberId,";
            $query .= "$this->key_Id,";
            $query .= "$this->timerFlg,";
            $query .= "$this->timerType,";
            $query .= "'$this->timerWeek',";
            $query .= "'$this->timerDate',";
            $query .= "$this->timerTime,";
            $query .= "$this->keywordStatus,";
            $query .= "$this->maxCpc,";
            $query .= "'$this->task',";
	    $query .= "'$now','$now')";
            $result = mysql_query($query);
            if(!$result)
            {
                return false;
            }
            else
            {
                $query = "SELECT  timerId ";
                $query .= "FROM   TimerKeywordSchedule ";
                $query .= "WHERE  registDate='$now'";
                $result = mysql_query($query);
                $row = mysql_fetch_array($result);
                return $row['timerId'];
            }
        }

        //タイマー更新
        function updateTimer()
        {
            $query = "UPDATE TimerKeywordSchedule SET ";
            $query .= "EnableFlg=$this->timerFlg,";
            $query .= "TimerType=$this->timerType,";
            $query .= "Week='$this->timerWeek',";
            $query .= "Date='$this->timerDate',";
            $query .= "Time=$this->timerTime,";
            $query .= "AppearFlg=$this->keywordStatus,";
            $query .= "maxCpc=$this->maxCpc,";
            $query .= "Task='$this->task',";
	    $query .= "lastUpdate=now() ";
            $query .= "WHERE ";
            $query .= "timerId=$this->timerId";
            $result = mysql_query($query);
            if(!$result)
            {
                return false;
            }
            return true;
        }

        //タイマー削除
        function deleteTimer($timerIds)
        {
            $query = "DELETE FROM TimerKeywordSchedule WHERE ";
            $query .= "timerId IN ($timerIds)";
            $result = mysql_query($query);
            if(!$result)
            {
                return false;
            }
            return true;
        }

        //タイマー検索
        function searchTimer()
        {
            //テーブルの一行の文字列
            $trs = "";
            $query = "SELECT * FROM TimerKeywordSchedule WHERE ";
            $query .= "key_Id=$this->key_Id";
            $result = mysql_query($query);
            if($result)
            {
                //行番号
                $i = 1;
                while($row = mysql_fetch_array($result))
                {
                    //タイマーID
                    $timerId = $row['TimerId'];
                    //タイマー実行状態
                    if($row['EnableFlg'] == 0)
                    {
                        $timerFlg = "オフ";
                    }
                    else
                    {
                        $timerFlg = "オン";
                    }
                    //日
                    $day = "";
                    //毎日の場合
                    if($row['TimerType'] == 0)
                    {
                        $day = "毎日";
                    }
                    //毎週の場合
                    else if($row['TimerType'] == 1)
                    {
                        $week = $row['Week'];
                        if(strpos($week,"0") !== False)
                        {
                            $day = "日";
                        }
                        if(strpos($week,"1") !== False)
                        {
                            if($day) $day .= ",月";else $day = "月";
                        }
                        if(strpos($week,"2") !== False)
                        {
                            if($day) $day .= ",火";else $day = "火";
                        }
                        if(strpos($week,"3") !== False)
                        {
                            if($day) $day .= ",水";else $day = "水";
                        }
                        if(strpos($week,"4") !== False)
                        {
                            if($day) $day .= ",木";else $day = "木";
                        }
                        if(strpos($week,"5") !== False)
                        {
                            if($day) $day .= ",金";else $day = "金";
                        }
                        if(strpos($week,"6") !== False)
                        {
                            if($day) $day .= ",土";else $day = "土";
                        }
                    }
                    //一回の場合
                    else if($row['TimerType'] == 2)
                    {
                        $day = $row['Date'];
                    }
                    //タイマー実行時間
                    $time = $row['Time']."時";
                    //タイマーの内容
                    $task = "";
                    if(strpos($row['Task'],"0") !== False)
                    {
                        if($row['AppearFlg'] == 0)
                        {
                            $task = "掲載を オフ に変更";
                        }
                        else
                        {
                            $task = "掲載を オン に変更";
                        }
                    }
                    if(strpos($row['Task'],"1") !== False)
                    {
                        if($task)
                        {
                            $task .= "<br>スポンサードサーチ入札額を {$row['maxCpc']} に変更";
                        }
                        else
                        {
                            $task = "スポンサードサーチ入札額を {$row['maxCpc']} に変更";
                        }
                    }

                    //画面のテーブルの文字列を作成
                    if($i%2 ==0)
                    {
                        $class = "tr3";
                    }
                    else
                    {
                        $class = "tr4";
                    }

                    $trs .= "<tr class=\"".$class."\">";
                    $trs .= "<td>$i</td>";
                    $trs .= "<td><input type=\"button\" class=\"btn_ico btn_edit\" title=\"編集\" value=\"\" onClick=\"editTimer(".$timerId.");\"></td>";
                    $trs .= "<td align=\"center\">$timerFlg</td>";
                    $trs .= "<td align=\"center\">$day</td>";
                    $trs .= "<td align=\"center\">$time</td>";
                    $trs .= "<td align=\"left\">$task</td>";
                    $trs .= "<td align=\"center\"><input name=\"timerCheck\" value=\"".$timerId."\" type=\"checkbox\"></td>";
                    $trs .= "</tr>";

                    $i++;
                }
                $this->count = $i-1;
            }
            return $trs;
        }

        //編集画面へ遷移するとき、編集画面を初期化
        function Init()
        {
            $query = "SELECT  * ";
            $query .= "FROM   TimerKeywordSchedule ";
            $query .= "WHERE  timerId=$this->timerId";
            $result = mysql_query($query);
            $row = mysql_fetch_array($result);

            $this->timerFlg = $row['EnableFlg'];
            $this->timerType = $row['TimerType'];
            $this->timerTime = $row['Time'];
            $this->timerWeek = $row['Week'];
            $this->timerDate = $row['Date'];
            $this->keywordStatus = $row['AppearFlg'];
            $this->maxCpc = $row['maxCpc'];
            $this->task = $row['Task'];
        }
    }

?>
